﻿/*
创建人：longb
日期：2022-05-24
描述：设备准备js
*/
var page = {
    pagetotal: 0,
    pageSize: 20,
    pageNumber: 1,
    editIndex: null, //easyUI单元格编辑
    rootPath: getRootPath(), //网站根路径
    ajaxUrl: getRootPath() + "/Ashx/ZsHandler/ZSIssueCollectMaterialHandler.ashx",
    //初始化页面
    init: function () {
        page.bind();
        customizedEasyuiPagination('pp'); //定制化分页
    },
    //页面事件绑定
    bind: function () {
        $('#Status').combobox({
            data: [{ id: 0, text: '待接收' }, { id: 1, text: '已接收' }, { id: 2, text: '配送中' }, { id: 3, text: '已完成'}],
            valueField: 'id',
            textField: 'text'
        });
        $('#Status').combobox('setValue', 0);
        //任务单信息
        $("#gdContainerInfo").datagrid({
            singleSelect: true,
            onLoadSuccess: function (data) {
                $('#pp').pagination('refresh', {	// 改变选项，并刷新分页栏信息
                    total: page.pagetotal,
                    pageSize: page.pageSize,
                    pageNumber: page.pageNumber
                });
                customizedEasyuiPagination('pp'); //定制化分页
            },
            onCheck: function (index, rowData) { page.GetWorkFlowData(); },
            onUncheck: function (index, rowData) { page.GetWorkFlowData(); },
            columns: [[
                 { field: 'ck', title: '选择', checkbox: true },
                 { field: 'ATTRIBUTEVALUE', title: '发动机号', width: 120 },
                 { field: 'MFGORDERNAME', title: '订单号', width: 150 },
		         { field: 'TOGETHERFLAG', title: '任务单号', width: 180 },
		         { field: 'WORKFLOWTYPE', title: '任务类型', width: 90 }, //--
                 {field: 'PRODUCTNAME', title: '件号', width: 120 },
                 { field: 'DESCRIPTION', title: '件号名称', width: 90 },
                 { field: 'WORKFLOWSTEPNAME', title: '阶段', width: 100 },
                 { field: 'WFDESC', title: '工艺名称', width: 100 },
                 { field: 'WORKFLOWSTEPNAME1', title: '工序号', width: 80 },
                 { field: 'WORKFLOWSTEPNAME2', title: '工序名称', width: 90 },
                 { field: 'PLANNEDSTARTDATE', title: '计划开始时间', width: 120, formatter: function (value, row, index) {
                     if (value != null)
                         return dayjs(value).format("YYYY-MM-DD");
                 }
                 },
                 { field: 'PLANNEDCOMPLETIONDATE', title: '计划完成时间', width: 120, formatter: function (value, row, index) {
                     if (value != null)
                         return dayjs(value).format("YYYY-MM-DD");
                 }
                 },
                 { field: 'TASKBILLSTATUS', title: '任务状态', width: 100 }
                 
	           ]]
        });
        //工序信息
        $('#EquXQTable').datagrid({
            fitColumns: true,
            singleSelect: true,
            columns: [[
		            { field: 'SPECNO', title: '设备编码', width: 50, align: 'center' },
		            { field: 'OPDESC', title: '设备名称', width: 50 },
                    { field: 'OPDESC1', title: '型号规格', width: 50 },
                    { field: 'OPDESC2', title: '设备类型', width: 50 },
                    { field: 'OPDESC3', title: '状态', width: 50 }
                ]],
            onSelect: function (rowIndex, rowData) {
                console.info(rowData);
                $('#EquTZTable').datagrid('loadData', [])
                page.GetBigSmallData(rowData.WORKFLOWSTEPID);
            }
        });
        //大小件信息
        $('#EquTZTable').datagrid({
            fitColumns: true,
            checkOnSelect: false,
            columns: [[
                    { field: 'ck', title: '选择', checkbox: true },
                    { field: 'OPDESC3', title: '设备编号', width: 50 },
                    { field: 'SPECNO', title: '设备编码', width: 50, align: 'center' },
		            { field: 'OPDESC', title: '设备名称', width: 50 },
                    { field: 'OPDESC1', title: '型号规格', width: 50 },
                    { field: 'OPDESC2', title: '设备状态', width: 50 }
		            ]],
            onCheck: function (index, rowData) { },
            onUncheck: function (index, rowData) { },
            onCheckAll: function (index, rowData) { },
            onUncheckAll: function (index, rowData) { },
        });
        //分页事件
        $('#pp').pagination({
            onSelectPage: function (pageNumber, pageSize) {
                customizedEasyuiPagination('pp'); //定制化分页
                page.pageSize = pageSize; page.pageNumber = pageNumber;
                page.GetTaskOrderData();
            }
        });
        //绑定分页go按钮
        $('#pageGo').on("click", function () {
            var pageNumber = $(this).parents('tr').find('.pagination-num').val();
            page.pageNumber = pageNumber;
            page.GetTaskOrderData();
        });
        //清除按钮
        $('#btnReset').on("click", function () {
            page.reset(true, true);
            page.pageNumber = 1; page.pagetotal = 0
            $('#pp').pagination('refresh', {
                total: page.pagetotal,
                pageSize: page.pageSize,
                pageNumber: page.pageNumber
            });
        });


    },
    //查询
    searchData: function () {
        page.reset(false, true);
        page.pageNumber = 1;
        $('#EquXQTable').datagrid('loadData', []);
        page.GetTaskOrderData();
    },
    //查询任务单信息
    GetTaskOrderData: function () {
        $('#EquXQTable').datagrid('loadData', [])
        $('#EquTZTable').datagrid('loadData', [])
        var frdata = $('#searchForm').serializeArray();
        var data = {};
        $.each(frdata, function () {
            data[this.name] = this.value
        });
        var param = {
            action: "GetTaskOrderData",
            type: page.pageType,
            pageSize: page.pageSize,
            pageNumber: page.pageNumber,
            data: JSON.stringify([data])
        };
        page.post(param, function (res) {
            if (res.Result) {
                console.info(res);
                page.pagetotal = res.RowCount;
                $('#gdContainerInfo').datagrid('loadData', res.DBTable);
            }
        });
    },
    //查询工序信息
    GetWorkFlowData: function () {
        $('#EquTZTable').datagrid('loadData', []);
        var CheckedRows = $('#gdContainerInfo').datagrid('getChecked');
        var WorkFlowId = ""
        if (CheckedRows.length > 0) {
            WorkFlowId = CheckedRows[0].WORKFLOWID;
        }
        if (WorkFlowId == "") { return; }
        var param = {
            action: "GetWorkFlowData",
            type: page.pageType,
            workflowid: WorkFlowId
        };
        page.post(param, function (res) {
            if (res.Result) {
                console.info(res);
                $('#EquXQTable').datagrid('loadData', res.DBTable)
            }
        });
    },
    //大小件信息
    GetBigSmallData: function (workflowstepid) {
        var frdata = $('#searchForm').serializeArray();
        var data = {};
        $.each(frdata, function () {
            data[this.name] = this.value
        });
        var strNoList = [];
        var CheckedRows = $('#gdContainerInfo').datagrid('getChecked');
        $.each(CheckedRows, function (index, item) {
            strNoList.push(item.TOGETHERFLAG);
        })
        var param = {
            action: "GetBigSmallData",
            type: page.pageType,
            workflowstepid: workflowstepid,
            strNoList: strNoList.join(','),
            data: JSON.stringify([data])
        };
        if (strNoList.length == 0) {
            //displayMessage("请选择任务单");
            $('#EquTZTable').datagrid('loadData', []);
            return;
        }
        page.post(param, function (res) {
            if (res.Result) {
                console.info(res);
                $('#EquXQTable').datagrid('loadData', res.DBTable)
            }
            //            displayMessage(res.Message, res.Result);
        });

    },
    //查询重置
    searchReset: function () {
        $('#searchForm').form('clear');
        page.bind();
    },
    //批量报工
    BatchIssueColloge: function (index) {
        var rows = $('#EquTZTable').datagrid('getChecked');
        if (index != null) {
            rows = [$('#EquTZTable').datagrid('getRows')[index]];
        }
        if (rows.length == 0) {
            displayMessage("请先勾选设备台账信息，再进行操作");
            return;
        }
        var data = [];
        $.each(rows, function (index, item) {
            if (item.CHANGEQTY <= item.CONFIRMQTY) {
                data.push(item);
            }
        });
        if (data.length == 0 || rows.length!=data.length) { displayMessage("XXX", false); return; }
        console.info(rows);
        var param = {
            action: "SaveIssueERPMaterialConfirmation",
            type: page.pageType,
            data: JSON.stringify(data)
        };
       
        page.post(param, function (res) {
            if (res.Result) {
                page.GetBigSmallData($('#EquXQTable').datagrid("getSelected").WORKFLOWSTEPID);
            }
            displayMessage(res.Message, res.Result);
        });

    },
    //清除方法，重要！！！
    reset: function (iscondition, isresult) {
        clearMessage();
    },
    //post数据
    post: function (param, callback) {
        $.ajax({
            type: "Post",
            url: page.ajaxUrl,
            data: param,
            cashe: false,
            async: true,
            dataType: "json",
            beforeSend: function () {
                $.messager.progress({ text: '', msg: '处理中...' });
            },
            success: function (data) {
                $.messager.progress('close');
                callback(data);
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                $.messager.progress('close');
                displayMessage(XMLHttpRequest.responseText, false);
            }
        });
    }
}
$(function () {
    page.init();
});